import {getProjects} from '@/services';
import {Toast} from 'antd-mobile'


export default {
    namespace: 'project',
    state: {
        orgId: "",
        searchText: "",
        list: [],
    },
    reducers: {
        handleList(state, {payload: {list}}) {
            state.list = list;
        },
        setSearchText(state, {payload: {value}}) {
            state.searchText = value;
        },
        setOrgId(state, {payload: {value}}) {
            state.orgId = value;
        },
    },
    effects: {
        * handleGetProjects(payload: any, {put, call, select}: any) {
            const orgId = yield select(state => state.orgId);
            const response = yield call(getProjects, orgId)
            console.log(response)
            const {code, message, data} = response;
            if (code === "0") {
                // 成功
                yield put({
                    type: "handleList",
                    payload: {
                        list: data
                    }
                })
            } else {
                Toast.show({
                    icon: 'fail',
                    content: message,
                })
            }

        },
    },
    subscriptions: {
        setup({dispatch}) {
            dispatch({
                type: 'handleGetProjects',
                payload: {
                    keywords: ""
                }
            });
        }
    },
};
